package com.woniu.uums.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.common.dto.UserDto;
import com.woniu.common.utils.PageData;
import com.woniu.common.utils.ResponseData;
import com.woniu.uums.dto.ZfPowerUserDto;
import com.woniu.uums.pojo.ZfPoweruser;
import com.woniu.uums.service.ZfPoweruserService;
import com.woniu.uums.mapper.ZfPoweruserMapper;
import io.lettuce.core.output.ScanOutput;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author HP
* @description 针对表【ZF_POWERUSER】的数据库操作Service实现
* @createDate 2024-11-21 15:34:24
*/
@Service
public class ZfPoweruserServiceImpl extends ServiceImpl<ZfPoweruserMapper, ZfPoweruser>
    implements ZfPoweruserService{
    @Autowired
    private ZfPoweruserMapper zfPoweruserMapper;



    @Override
    public String login(String username, String password) {
       //md5加密
        password = SecureUtil.md5(SecureUtil.md5(password) + "woniu");

        ZfPoweruser user = zfPoweruserMapper.queryLogin(username, password);

        if (ObjectUtil.isEmpty(user)) {
            return null;
        }

        //脱敏
//        user.setFinancePwd(null);
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(user, userDto);

        //生成jwt
        Map<String, Object> payload = new HashMap<>(1);
        payload.put("user", userDto);
        return JWTUtil.createToken(payload, "woniu".getBytes());

    }

    @Override
    public String login2(String username, String password) {
          //md5加密
        password = SecureUtil.md5(SecureUtil.md5(password) + "woniu");

        ZfPoweruser user = lambdaQuery().eq(ZfPoweruser::getUsername, username)
                .eq(ZfPoweruser::getPassword, password)
                .one();

        if (ObjectUtil.isEmpty(user)) {
            return null;
        }

        //脱敏
//        user.setFinancePwd(null);
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(user, userDto);

        //生成jwt
        Map<String, Object> payload = new HashMap<>(1);
        payload.put("user", userDto);
        return JWTUtil.createToken(payload, "woniu".getBytes());
    }

    @Override
    public ResponseData<?> queryAll(PageData<ZfPowerUserDto> pageData) {
        PageHelper.startPage(pageData.getPageNum(), pageData.getPageSize());
        List<ZfPowerUserDto> zfPowerUserDtos = zfPoweruserMapper.queryAll(pageData.getParam());
        PageInfo pageInfo = new PageInfo(zfPowerUserDtos);
        return new ResponseData<>().ok(pageInfo);
    }

    @Override
    public ZfPowerUserDto queryByCode(String code) {
        return zfPoweruserMapper.queryByCode(code);
    }

    @Override
    public ResponseData<?> queryByRoleId(String roleId) {
        return new ResponseData<>().ok(zfPoweruserMapper.queryByRoleId(roleId));
    }
}